<?php

namespace App\Controllers;

use CodeIgniter\Controller;
use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use App\Models\User_model;

/**
 * Class BaseController
 *
 * BaseController provides a convenient place for loading components
 * and performing functions that are needed by all your controllers.
 * Extend this class in any new controllers:
 *     class Home extends BaseController
 *
 * For security be sure to declare any new methods as protected or private.
 */
class BaseController extends Controller
{
    /**
     * Instance of the main Request object.
     *
     * @var CLIRequest|IncomingRequest
     */
    protected $request;

    /**
     * An array of helpers to be loaded automatically upon
     * class instantiation. These helpers will be available
     * to all other controllers that extend BaseController.
     *
     * @var array
     */
    protected $helpers = [];
    protected $userToken;//用户令牌
    /**
     * @var \CodeIgniter\Session\Session
     */
    protected $session;
    /**
     * @var User_model
     */
    protected $user_model;
    protected $avatar_path = './uploads/images/avatar/';
    protected $article_path = './uploads/images/article/';
    protected $qualification_path = './uploads/images/qualification/';
    /**
     * @var \CodeIgniter\Validation\Validation
     */
    protected $validation;

    public function __construct(){

        //header('Access-Control-Allow-Methods:*');
        // 响应头设置
        //header('Access-Control-Allow-Headers:x-requested-with,content-type');
    }

    /**
     * Constructor.
     */
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request, $response, $logger);
        //header("Access-Control-Allow-Origin:".$request->header("Origin"));
        header("Access-Control-Allow-Origin:*");
        header('Access-Control-Allow-Methods:*');
        header('Access-Control-Allow-Credentials:true');
        // 响应头设置
        //header('Access-Control-Allow-Headers:x-requested-with,content-type');
        //header('Set-Cookie: HttpOnly; SameSite=None;Secure=false');

        $this->session = \Config\Services::session();
        $this->user_model = new user_model();

        $this->validation = \Config\Services::validation(); //加载表单验证类库
        helper(['form', 'url']);

        //$this->$userToken = $this->request->getHeaderLine('token');

        // Preload any models, libraries, etc, here.

        // E.g.: $this->session = \Config\Services::session();
    }
    public function json_result(bool $type, string $msg, array $result)
    {
        if($type === true){
            $data=array(
                'code'=>200,
                'msg'=>$msg,
                'data'=>$result
            );
        } else {
            $data=array(
                'code'=>400,
                'msg'=>$msg,
                'data'=>$result
            );
        }
        return json_encode($data);
    }

    // 废弃
    public function getUserInfo()
    {
        $cache = $this->cache->get($this->userToken);
        if (!$cache) {
            $this->userid = 0;
            $this->user = null;
            return '';
        }
        if (isset($cache['userid']) && $cache['userid']) {
            $this->userid = $cache['userid'];
            $this->session->set('userid', $this->userid);
            $this->user = $this->user_model->getUser($this->userid);
            $this->renderer->setData(['user' => $this->user, 'userid' => $this->userid]);
        }
    }

    public function is_login(): bool
    {
        if($this->session->has('u_id') && $this->session->get('u_id')!='')
            return true;
        else
            return false;
    }
}
